Vue.createApp({
    data() {
        return {
            moreHint: '',
            moreType: 'takeaway',
            totalInfo: {
                phone: '',
                type: '',
                blockCount: -1,
                groupCount: -1,
                senderCount: -1,
                adminCount: -1,
                takeawayCount: -1
            },
            blocks: [],
            groups: [],
            takeawayInfo: {
                userName: '',
                phone: '',
                detail: '',
                blockName: '',
                groupName: ''
            },
            senderInfo: {
                name: '',
                phone: '',
                password: '',
                blockName: '',
                groupName: ''
            },
            adminInfo: {
                name: '',
                phone: '',
                password: '',
                blockName: '',
            },
            groupInfo: {
                groupName: '',
                info: '',
                blockName: '',
            },
            blockInfo: {
                blockName: '',
                info: '',
            },


        }
    },
    methods: {
        changeBlockSelect() {
            if (this.blocks.length !== this.totalInfo.blockCount) {
                axios.get("/more/get/block").then(resp => {
                    this.blocks = resp.data;
                });
            }
        },
        changeGroupSelect(type) {
            let blockName = "";
            if (type === "takeaway") {
                blockName = this.takeawayInfo.blockName;
            } else if (type === "sender") {
                blockName = this.senderInfo.blockName;
            }
            axios.get("/page/get/group?blockName=" + blockName).then(resp => {
                this.groups = resp.data;
            });
        },
        getTotalInfoAndShow(show) {
            axios.get("/more/get").then(resp => {
                this.totalInfo = resp.data;
                if (show)
                    this.showView();
            });
        },
        showView() {
            if (this.totalInfo.type === "sender") {
                document.getElementById("blockCount").remove();
                document.getElementById("groupCount").remove();
                document.getElementById("adminCount").remove();
                document.getElementById("senderCount").remove();
                document.getElementById("addBlockTag").remove();
                document.getElementById("addGroupTag").remove();
                document.getElementById("addAdminTag").remove();
                document.getElementById("addSenderTag").remove();
            } else if (this.totalInfo.type === "admin") {
                document.getElementById("adminCount").remove();
                document.getElementById("blockCount").remove();
                document.getElementById("addBlockTag").remove();
                document.getElementById("addAdminTag").remove();
            }
        },
        addTakeaway() {
            if (!(new RegExp("^\\d{11}$")).test(this.takeawayInfo.phone)) {
                this.moreHint = "请正确输入手机号码";
                return;
            }

            if (!(new RegExp("^[\u4E00-\u9FA5A-Za-z0-9_]*$")).test(this.takeawayInfo.userName)) {
                this.moreHint = "收货人姓名不应该包含特殊字符和空格";
                return;
            }

            if (this.takeawayInfo.userName.length < 0 || this.takeawayInfo.userName.length > 20) {
                this.moreHint = "收货人姓名长度应在1至20位";
                return;
            }
            if (this.takeawayInfo.detail.length > 200) {
                this.moreHint = "备注长度应在1至200位";
                return;
            }

            if (this.takeawayInfo.blockName.length <= 0) {
                this.moreHint = "请选择区域";
                return;
            }

            if (this.takeawayInfo.groupName.length <= 0) {
                this.moreHint = "请选择组别";
                return;
            }

            axios.post("/more/add/takeaway", this.takeawayInfo).then(resp => {
                if (resp.data === "OK") {
                    this.moreHint = "添加外卖配送信息成功，收货号码: " + this.takeawayInfo.phone;
                    this.takeawayInfo = {
                        userName: '',
                        phone: '',
                        detail: '',
                        blockName: '',
                        groupName: ''
                    };
                    this.getTotalInfoAndShow(false);
                } else {
                    this.moreHint = "未知错误导致失败!";
                }
            }).catch(ex => {
                this.moreHint = "未知错误导致失败!";
            });

        },
        addSender() {
            if (!(new RegExp("^\\d{11}$")).test(this.senderInfo.phone)) {
                this.moreHint = "请正确输入手机号码";
                return;
            }

            if (!(new RegExp("[a-zA-Z\\d#*+._-]{6,18}")).test(this.senderInfo.password)) {
                this.moreHint = "请正确输入密码(6-18位, 允许特殊符号#*+._-)";
                return;
            }

            if (!(new RegExp("^[\u4E00-\u9FA5A-Za-z0-9_]*$")).test(this.senderInfo.name)) {
                this.moreHint = "姓名不应该包含特殊字符和空格";
                return;
            }

            if (this.senderInfo.name.length < 0 || this.senderInfo.name.length > 20) {
                this.moreHint = "姓名长度应在1至20位";
                return;
            }

            if (this.senderInfo.blockName.length <= 0) {
                this.moreHint = "请选择区域";
                return;
            }

            if (this.senderInfo.groupName.length <= 0) {
                this.moreHint = "请选择组别";
                return;
            }

            axios.post("/more/add/sender", this.senderInfo).then(resp => {
                if (resp.data === "OK") {
                    this.moreHint = "添加配送员成功，配送员手机号码: " + this.senderInfo.phone;
                    this.senderInfo = {
                        name: '',
                        phone: '',
                        password: '',
                        blockName: '',
                        groupName: ''
                    };
                    this.getTotalInfoAndShow(false);
                } else {
                    let data = resp.data.split(":");
                    if (data.length > 1 && data[1] === "Exist")
                        this.moreHint = "该配送员已存在";
                    else
                        this.moreHint = "未知错误导致失败!";
                }
            }).catch(ex => {
                this.moreHint = "未知错误导致失败!";
            });

        },
        addAdmin() {
            if (!(new RegExp("^\\d{11}$")).test(this.adminInfo.phone)) {
                this.moreHint = "请正确输入手机号码";
                return;
            }

            if (!(new RegExp("[a-zA-Z\\d#*+._-]{6,18}")).test(this.adminInfo.password)) {
                this.moreHint = "请正确输入密码(6-18位, 允许特殊符号#*+._-)";
                return;
            }

            if (!(new RegExp("^[\u4E00-\u9FA5A-Za-z0-9_]*$")).test(this.adminInfo.name)) {
                this.moreHint = "姓名不应该包含特殊字符和空格";
                return;
            }

            if (this.adminInfo.name.length > 20) {
                this.moreHint = "姓名长度应在1至20位";
                return;
            }

            if (this.adminInfo.blockName.length <= 0) {
                this.moreHint = "请选择区域";
                return;
            }

            axios.post("/more/add/admin", this.adminInfo).then(resp => {
                if (resp.data === "OK") {
                    this.moreHint = "添加管理员成功，管理员手机号码: " + this.adminInfo.phone;
                    this.adminInfo = {
                        name: '',
                        phone: '',
                        password: '',
                        blockName: '',
                    };
                    this.getTotalInfoAndShow(false);
                } else {
                    let data = resp.data.split(":");
                    if (data.length > 1 && data[1] === "Exist")
                        this.moreHint = "该管理员已存在";
                    else
                        this.moreHint = "未知错误导致失败!";
                }
            }).catch(ex => {
                this.moreHint = "未知错误导致失败!";
            });

        },
        addGroup() {
            if (!(new RegExp("^[\u4E00-\u9FA5A-Za-z0-9_]+$")).test(this.groupInfo.groupName)) {
                this.moreHint = "名称不应该包含特殊字符和空格";
                return;
            }

            if (this.groupInfo.groupName.length > 20) {
                this.moreHint = "名称长度应在1至20位";
                return;
            }

            if (this.groupInfo.info.length > 200) {
                this.moreHint = "详细长度应在1至200位";
                return;
            }

            if (this.groupInfo.blockName.length <= 0) {
                this.moreHint = "请选择区域";
                return;
            }

            axios.post("/more/add/group", this.groupInfo).then(resp => {
                if (resp.data === "OK") {
                    this.moreHint = "添加组别成功，组别: " + this.groupInfo.groupName;
                    this.groupInfo = {
                        groupName: '',
                        info: '',
                        blockName: '',
                    };
                    this.getTotalInfoAndShow(false);
                } else {
                    let data = resp.data.split(":");
                    if (data.length > 1 && data[1] === "Exist")
                        this.moreHint = "该组别已存在";
                    else
                        this.moreHint = "未知错误导致失败!";
                }
            }).catch(ex => {
                this.moreHint = "未知错误导致失败!";
            });

        },
        addBlock() {
            if (!(new RegExp("^[\u4E00-\u9FA5A-Za-z0-9_]+$")).test(this.blockInfo.blockName)) {
                this.moreHint = "名称不应该包含特殊字符和空格";
                return;
            }

            if (this.blockInfo.blockName.length > 20) {
                this.moreHint = "名称长度应在1至20位";
                return;
            }

            if (this.blockInfo.info.length > 200) {
                this.moreHint = "详细长度应在1至200位";
                return;
            }

            axios.post("/more/add/block", this.blockInfo).then(resp => {
                if (resp.data === "OK") {
                    this.moreHint = "添加组别成功，区域: " + this.blockInfo.blockName;
                    this.blockInfo = {
                        blockName: '',
                        info: '',
                    };
                    this.getTotalInfoAndShow(false);
                } else {
                    let data = resp.data.split(":");
                    if (data.length > 1 && data[1] === "Exist")
                        this.moreHint = "该区域已存在";
                    else
                        this.moreHint = "未知错误导致失败!";
                }
            }).catch(ex => {
                this.moreHint = "未知错误导致失败!";
            });
        },
    },
    mounted() {
        this.getTotalInfoAndShow(true);
    },
    watch: {
        moreType(n, o) {
            this.moreHint = '';
            this.takeawayInfo = {
                userName: '',
                phone: '',
                detail: '',
                blockName: '',
                groupName: ''
            };
            this.senderInfo = {
                name: '',
                phone: '',
                password: '',
                blockName: '',
                groupName: ''
            };
            this.adminInfo = {
                name: '',
                phone: '',
                password: '',
                blockName: '',
            };
            this.groupInfo = {
                groupName: '',
                info: '',
                blockName: '',
            };
            this.blockInfo = {
                blockName: '',
                info: '',
            };
        }
    }
}).mount("#more-app");